import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/router/login'
import HomeRoter from  '@/views/Homes/router/childrens'
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/Home',
        name: 'home',
        meta: {
            login: true,
            title: '首页'
        },
        children: [
            ...HomeRoter
        ],
        component: () => import('../views/Home'),
        redirect: '/welcome'
    },
    ...Login
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  if(typeof(location)=="string"){
    var Separator = "&";
    if(location.indexOf('?')==-1) { Separator='?'; }
    location = location + Separator + "random=" + Math.random();
  }
  return routerPush.call(this, location).catch(error=> error)
}

router.beforeEach((to, from, next) => {
        if (to.meta.login) {
            if (sessionStorage.getItem('token')) {
                next()
            } else {
                next('/')
            }
        } else {
            next()
        }
        if (to.meta.title) {
            document.title = to.meta.title
        }
        next()

    }
)
export default router
